package com.da.lon.wang.xlg.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.FixAddressAdapter;
import com.da.lon.wang.xlg.bean.AddressList;
import com.da.lon.wang.xlg.bean.AddressListBean;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.Mode;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshListView;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;
import java.util.List;

public class FixAddressActivity extends BaseActivity implements
        OnRefreshListener2<ListView>, OnClickListener {

    private PullToRefreshListView listView;
    private String id;
    private int page;

    private HttpLoader<AddressListBean> loader;
    private List<AddressList> list = new ArrayList<AddressList>();

    private ImageButton backButton;
    private Button addButton;

    private FixAddressAdapter adapter;

    private Context context;
    private String pro_type;

    @Override
    protected void onResume() {
        super.onResume();
        addButton.setEnabled(true);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ouka_address_fix);

        context = this;
        pro_type = getIntent().getStringExtra("pro_type");
        backButton = (ImageButton) findViewById(R.id.ouka_address_left);
        addButton = (Button) findViewById(R.id.ouka_address_add);

        backButton.setOnClickListener(this);
        addButton.setOnClickListener(this);

    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        initView();
    }

    private void initView() {
        // TODO Auto-generated method stub
        try {
            listView = (PullToRefreshListView) findViewById(R.id.ouka_address_listview);
            listView.setMode(Mode.BOTH);
            listView.setOnRefreshListener(this);

            adapter = new FixAddressAdapter(list, context);
            listView.getRefreshableView().setAdapter(adapter);

            id = MyApplication.getInstance().getSharedPreferences("user_info", MODE_PRIVATE).getString("user_id", "");

            if ("".equals(id)) {
                Intent loginIntent = new Intent(context, LoginActivity.class);
                loginIntent.putExtra("isGotoHome", true);
                ((Activity) context).startActivityForResult(loginIntent, 100);
                finish();
            }

            listView.getRefreshableView().setOnItemClickListener(
                    new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int position, long id) {
                            // TODO Auto-generated method stub

                            AddressList bean = list.get(position - 1);
                            Intent intent = new Intent(FixAddressActivity.this,
                                    EditAddressActivity.class);
                            intent.putExtra("adname", bean.getReal_name());
                            intent.putExtra("adphone", bean.getTelphone());

                            if (bean != null && bean.getCounty_name() == null) {
                                intent.putExtra(
                                        "adlocation",
                                        bean.getPro_name() + ","
                                                + bean.getCity_name());
                            } else {
                                intent.putExtra(
                                        "adlocation",
                                        bean.getPro_name() + ","
                                                + bean.getCity_name() + ","
                                                + bean.getCounty_name());
                            }

                            intent.putExtra("adaddress", bean.getAddress());
                            intent.putExtra("addid", bean.getAdd_id());
                            intent.putExtra("bodycard",
                                    bean.getBody_card_number());
                            intent.putExtra("proviceid", bean.getProvinceid()
                                    + "");
                            intent.putExtra("cityid", bean.getCity() + "");
                            intent.putExtra("areaid", bean.getCounty() + "");
                            intent.putExtra("pro_type", pro_type);
                            intent.putExtra("key", 1);
                            startActivity(intent);

                        }
                    });

            page = 1;

            loader = new HttpLoader<AddressListBean>(this, XLGConstants.URL
                    + "Shipping-addresslist", AddressListBean.class, 60 * 1000);
            AjaxParams params = new AjaxParams();
            params.put("page", page + "");
            params.put("id", id);
            params.put("token", XLGDataUtils.encrypt2MD5("Shipping" + "2015#$%"
                    + "addresslist"));

            loader.setManagerListener(new OnWebLoadListener<AddressListBean>() {

                @Override
                public void OnStart() {
                    // TODO Auto-generated method stub
                    showDialog();
                }

                @Override
                public void OnError(String error) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                }

                @Override
                public void onSuccess(AddressListBean t) {
                    // TODO Auto-generated method stub
                    dismissDialog();

                    if (t != null && t.getCode().equals("000000")) {
                        if (t.getResult() == null) {
                            list.clear();
                            adapter.notifyDataSetChanged();
                        } else {
                            list.clear();
                            list.addAll(t.getResult());
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            });
            loader.loadData(params);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        // TODO Auto-generated method stub
        try {
            page = 1;

            loader = new HttpLoader<AddressListBean>(this, XLGConstants.URL
                    + "Shipping-addresslist", AddressListBean.class, 60 * 1000);
            AjaxParams params = new AjaxParams();
            params.put("page", page + "");
            params.put("id", id);
            params.put("token", XLGDataUtils.encrypt2MD5("Shipping" + "2015#$%"
                    + "addresslist"));

            loader.setManagerListener(new OnWebLoadListener<AddressListBean>() {

                @Override
                public void OnStart() {
                    // TODO Auto-generated method stub
                    showDialog();
                }

                @Override
                public void OnError(String error) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            listView.onRefreshComplete();
                        }
                    });
                }

                @Override
                public void onSuccess(AddressListBean t) {
                    // TODO Auto-generated method stub
                    dismissDialog();

                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            listView.onRefreshComplete();
                        }
                    });

                    if (t != null && t.getCode().equals("000000")) {

                        if (t.getResult() == null) {
                            list.clear();
                            adapter.notifyDataSetChanged();
                        } else {
                            list.clear();
                            list.addAll(t.getResult());
                            adapter.notifyDataSetChanged();
                        }
                    }
                }
            });
            loader.loadData(params);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        // TODO Auto-generated method stub

        loader = new HttpLoader<AddressListBean>(this, XLGConstants.URL
                + "Shipping-addresslist", AddressListBean.class, 60 * 1000);
        AjaxParams params = new AjaxParams();
        params.put("page", page + 1 + "");
        params.put("id", id);
        params.put("token", XLGDataUtils.encrypt2MD5("Shipping" + "2015#$%"
                + "addresslist"));

        loader.setManagerListener(new OnWebLoadListener<AddressListBean>() {

            @Override
            public void OnStart() {
                // TODO Auto-generated method stub
                showDialog();
            }

            @Override
            public void OnError(String error) {
                // TODO Auto-generated method stub
                dismissDialog();
                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        listView.onRefreshComplete();
                    }
                });
            }

            @Override
            public void onSuccess(AddressListBean t) {
                // TODO Auto-generated method stub
                dismissDialog();

                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        listView.onRefreshComplete();
                    }
                });

                if (t != null && t.getCode().equals("000000")) {

                    if (t.getResult() != null && t.getResult().size() != 0) {
                        page = page + 1;
                        list.addAll(t.getResult());
                    }
                    adapter.notifyDataSetChanged();
                }
            }
        });
        loader.loadData(params);


    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
            case R.id.ouka_address_left:
                finish();
                break;

            case R.id.ouka_address_add:
                addButton.setEnabled(false);
                Intent intent = new Intent(FixAddressActivity.this,
                        EditAddressActivity.class);
                intent.putExtra("key", 0);
                intent.putExtra("pro_type", pro_type);
                startActivity(intent);
                break;

            default:
                break;
        }

    }
}
